package com.topInterview;

/**
 * @Author: huangzhigao
 * @Date: 2022/2/20 19:58
 */
public class Leecode101_symmetryTree {
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    public boolean isSymmetric(TreeNode root) {
        return isMirror(root, root);
    }

    //head1为原树，head2为镜像树
    public boolean isMirror(TreeNode head1, TreeNode head2) {
        if (head1 == null && head2 == null) {
            return true;
        }
        if (head1 != null && head2 != null) {
            //两个头要相等，head1的左孩子和head2的有孩子需要为镜像树
            return head1.val == head2.val && isMirror(head1.left, head2.right) && isMirror(head1.right, head2.left);
        }

        return false;


    }
}
